/*
 * unittest3.c
 * make a set of random tests
 * to see cover as much of each function as possible.
 * This test will be for getWinners
 * I will exclude invalid input for now.
 * Help was from class examples, badTestDrawCard.c
 * and an example of ramdom testing from a previous class
 * found at http://code.google.com/p/cs362s2013/source/browse/trunk/lutzjo/dominion/unittest1.c
 *  Created on: Jan 25, 2014
 *      Author: Charles_OSU
 */



#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "rngs.h"
#include <math.h>

#define DEBUG 0

// Prototype
int gainCardtest(int supplyPos, struct gameState *state, int toFlag, int player);

int gainCardtest(int supplyPos, struct gameState *state, int toFlag, int player){

	 int numErrors = 0;
	 int result = 0;
	 struct gameState gsAfter;
	      memcpy(&gsAfter, state, sizeof(struct gameState));

	 //Note: supplyPos is enum of choosen card

	      // get the result first then test for errors
	      result = gainCard(supplyPos, state, toFlag, player);


	  //check if supply pile is empty (0) or card is not used in game (-1)
	  if ( supplyCount(supplyPos, &gsAfter) < 1 )
	    {
	      return -1;
	    }

	  //added card for [whoseTurn] current player:
	  // toFlag = 0 : add to discard
	  // toFlag = 1 : add to deck
	  // toFlag = 2 : add to hand

	  if (toFlag == 1)
	    {
		  gsAfter.deck[ player ][ gsAfter.deckCount[player] ] = supplyPos;
		  gsAfter.deckCount[player]++;
	    }
	  else if (toFlag == 2)
	    {
		  gsAfter.hand[ player ][ gsAfter.handCount[player] ] = supplyPos;
		  gsAfter.handCount[player]++;
	    }
	  else
	    {
		  gsAfter.discard[player][ gsAfter.discardCount[player] ] = supplyPos;
		  gsAfter.discardCount[player]++;
	    }

	  //decrease number in supply pile
	  gsAfter.supplyCount[supplyPos]--;

	  if(result != 0){
	      printf("Our result of %d did not match our expectations of 0 \n",result);
	      printf("Our paramaters at the time were result = %d \n", result);
	      numErrors += 1; //did not match, add one to the error
	    }
	    if(memcmp(&gsAfter, state, sizeof(struct gameState)) != 0){
	      printf("Our gameStates do not match! \n");
	      numErrors += 1; //Gamestates did not match so add one to numErrors
	    }

	  return numErrors;
}




int main() {

   // Create main game play setup including structs,
  // variables, and any state I need to emulate

  struct gameState gameStateTest;//testing gameStateTest

  int x, y, z;
  int playersTest[MAX_PLAYERS];
  int players;  // players
  int minPlayers=2;
  int maxPlayers=4;
  int supplyPos;
  int toFlag;
  int testNums = 0;
  int numErrors = 0;
  int k[10] = {adventurer, council_room, feast, gardens, mine,
 	       remodel, smithy, village, baron, great_hall};

  // Set the random number of players
  //players = (int)floor(Random() * maxPlayers);

 // now seed the game
  SelectStream(2);
  PutSeed(3);


  // Print to our screen which tests are being run
  // which will get directed to out .out file later
  // when we add commands to the makefile to redirect
  // the output

  printf("Testing gainCardtest begin\n");
 // printf ("%d RANDOM TESTS.\n", testNums);

  // Additional setup and initialization. Some code
  // borrowed from badTestDrawCard.c

  for (x = 0; x < 3000; x++) {
      for (y = 0; y < sizeof(struct gameState); y++) {
           ((char*)&gameStateTest)[y] = floor(Random() * 256);
         }

      // Set the random number of players
       players = (int)floor(Random() * maxPlayers);

      // Setup my players decks and hands
       gameStateTest.deckCount[players] = floor(Random() * MAX_DECK);
       gameStateTest.discardCount[players] = floor(Random() * MAX_DECK);
       gameStateTest.handCount[players] = floor(Random() * MAX_HAND);



      for(z=0;z<gameStateTest.handCount[players];z++)
      {

    	  gameStateTest.hand[players][z] = (int)(Random() * MAX_HAND);

      }

      for(z=0;z<gameStateTest.discardCount[players];z++)
      	  {

         	  gameStateTest.discard[players][z] = (int)(Random() * MAX_HAND);

           }

      for(z=0; z<gameStateTest.deckCount[players]; z++)
          {

            gameStateTest.deck[players][z] = (int)(Random() * MAX_HAND);

          }

      supplyPos = floor(Random() * 25);
      toFlag = floor(Random() * 3);

  //          printf ("Initialization and setup completed now running tests for gainCardtest and counting errors.\n");
      numErrors +=   gainCardtest(supplyPos, &gameStateTest, toFlag, players);
      testNums += 1;
    }


  // If we get to here it means we kept running and did not crash the program
  // so our tests must have run now let's see how many errors we got.
    printf ("All Tests ran for gainCardtest\n");
    printf ("%d RANDOM TESTS.\n", testNums);


if (numErrors > 0) { printf("There were %d errors found for gainCardtest. ", numErrors);}
else { printf("There were no errors found in gainCardtest.");}


return 0;

}  // End main
